import { loginApi } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
export default {
  // 开启模块化
  namespaced: true,
  state: {
    token: getToken(),
  },
  mutations: {
    // 设置token
    setToken(state, data) {
      // 存入token
      state.token = data;
      // 存入cookie实现持久化
      setToken(data);
    },
    // 删除token
    removeToken(state) {
      state.token = null;
      removeToken();
    },
  },
  actions: {
    // 发送请求的action
    async loginAction(context, data) {
      const res = await loginApi(data);
      context.commit("setToken", res);
    },
  },
};
